package org.yangspring.samples.mvc.daoOracle;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.yangspring.samples.mvc.domainOracle.Vdatafile;

import java.util.List;

/**
 * Created by franklin on 6/24/2017.
 */

@Repository
public class VdatafileDAOImpl implements VdatafileDAO {

    private static final Logger logger = LoggerFactory.getLogger(SalgradeDAO.class);

    @Autowired
    @Qualifier("hibernate5AnnotatedSessionFactoryOracle")
    private SessionFactory sessionFactory;
    /*public void setSessionFactory(SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
    }*/


    public List<Vdatafile> listVdatefile(){

//        Session session = this.sessionFactory.getCurrentSession();
        Session session = this.sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(this.sessionFactory, session);

//        List<Vdatafile> vdfList2 = session.createQuery("from Vdatafile").list();

        //  标量查询，But标量查询有个问题，JSP不好处理
//        List<Vdatafile> vdfList = session.createSQLQuery("select * from SYS.V_$DATAFILE").addScalar("NAME", StringType.INSTANCE).addScalar("FILE#", IntegerType.INSTANCE).addScalar("CREATION_TIME", DateType.INSTANCE).addScalar("TS#", IntegerType.INSTANCE).addScalar("RFILE#", IntegerType.INSTANCE).addScalar("STATUS", StringType.INSTANCE).addScalar("ENABLED", StringType.INSTANCE).addScalar("CHECKPOINT_TIME", DateType.INSTANCE).addScalar("ONLINE_TIME", DateType.INSTANCE).addScalar("BYTES", IntegerType.INSTANCE).addScalar("BLOCKS", IntegerType.INSTANCE).list();

        //  实体查询
        List<Vdatafile> vdfList = session.createSQLQuery("select NAME, FILE#, CREATION_TIME, TS#, RFILE#, STATUS," +
                "ENABLED, CHECKPOINT_TIME, ONLINE_TIME, BYTES, BLOCKS " +
                /*, CREATE_BYTES, BLOCK_SIZE, PLUGGED_IN, BLOCK1_OFFSET, AUX_NAME*/
                "from SYS.V_$DATAFILE").addEntity(Vdatafile.class).list();

        //  标量查询时，这里的logger功能不行
//        for(Vdatafile df : vdfList){
//            logger.info("Vdatafile List::" + df);
//        }
        return vdfList;
    }

}
